/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.kelin.calendarlistview.library;

import ohos.agp.components.*;

import ohos.app.Context;

/**
 * ListView
 */
public class ListView extends DirectionalLayout {
    private DirectionalLayout mHeader;
    private ListContainer mBody;
    private DirectionalLayout mFooter;

    /**
     * ListView instance
     *
     * @param context 上下文
     */
    public ListView(Context context) {
        super(context);
        init(context);
    }

    /**
     * ListView instance
     *
     * @param context 上下文
     * @param attrSet 属性
     */
    public ListView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        init(context);
    }

    /**
     * ListView instance
     *
     * @param context 上下文
     * @param attrSet 属性
     * @param styleName 名称
     */
    public ListView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init(context);
    }

    private void init(Context context) {
        LayoutScatter.getInstance(context).parse(ResourceTable.Layout_listview, this, true);
        mHeader = (DirectionalLayout) findComponentById(ResourceTable.Id_header);
        mBody = (ListContainer) findComponentById(ResourceTable.Id_body);
        mBody.enableScrollBar(AXIS_Y, true);
        mBody.setLongClickable(false);
        mFooter = (DirectionalLayout) findComponentById(ResourceTable.Id_footer);
    }

    /**
     * getHeader
     * @return DirectionalLayout
     */
    public DirectionalLayout getHeader() {
        return mHeader;
    }

    /**
     * getBody
     * @return ListContainer
     */
    public ListContainer getBody() {
        return mBody;
    }

    /**
     * getFooter
     * @return DirectionalLayout
     */
    public DirectionalLayout getFooter() {
        return mFooter;
    }

    /**
     * getHeaderViewsCount
     * @return int
     */
    public int getHeaderViewsCount() {
        return mHeader.getChildCount();
    }

    /**
     * getFooterViewsCount
     * @return int
     */
    public int getFooterViewsCount() {
        return mFooter.getChildCount();
    }

    /**
     * addHeaderView
     * @param view Component
     */
    public void addHeaderView(Component view) {
        mHeader.addComponent(view);
    }

    /**
     * addFooterView
     * @param view Component
     */
    public void addFooterView(Component view) {
        mFooter.setMarginTop(-view.getHeight());
        mFooter.addComponent(view);
    }

    /**
     * removeHeaderView
     * @param view Component
     */
    public void removeHeaderView(Component view) {
        mHeader.removeComponent(view);
    }

    /**
     * removeFooterView
     * @param view Component
     */
    public void removeFooterView(Component view) {
        mFooter.removeComponent(view);
    }
}
